<template>
    <view>
        <SearchHeader :keyword="keyword" @change="searchChange"/>
        <CheckTitle :titleList="titleList" :checkIndex="checkIndex" @checkChange="checkChange"/>
        <scroll-view
            class="pro_main"
            scroll-y="true"
            :style="{'height': `${noBarHeight - 52}px`}"
            :enhanced="true"
            :show-scrollbar="false"
            @scrolltolower="lower"
        >
            <view class="view_block" v-if="pageType == 'project'">
                <ProBlock
                    @onClick="navProject"
                    v-for="(item, index) in dataList"
                    :key="index"
                    :dataDetail="item"
                />
            </view>
            <view class="view_block" v-else-if="pageType == 'company'">
                <CompanyBlack
                    @onClick="navCompany"
                    v-for="(item, index) in dataList"
                    :key="index"
                    :dataDetail="item"
                />
            </view>
            <NoMessage v-show="dataList.length == 0" />
        </scroll-view>
    </view>
</template>
<script>
// import {mobilesubscription} from "@/utils/api/project";
// import {mobsearch} from "@/utils/api/company";


import {mobsearch} from "../../../utils/api/company";

var app = getApp()
import SearchHeader from "../../components/SearchHeader.vue"
import CheckTitle from "@/components/CheckTitle.vue"
import CompanyBlack from "@/components/CompanyBlock.vue"
import ProBlock from "@/components/ProBlock.vue";
import NoMessage from "@/components/NoMessage"
import {mobilesubscription} from "../../../utils/api/project";
export default ({
    name: "company",
    data() {
        return {
            background: 'linear-gradient(0, #5162FD, #8E4EF7)',
            title: '企业',
            noBarHeight: app.globalData.noBarHeight,
            footerRail: app.globalData.footerRail,
            screenIndex: 0,
            keyword: '',
            searchType: 'firm',
            pages: {
                total: 0,
                size: 20,
                page: 1,
                isOver: false
            },
            dataList: [],
            checkIndex: 1,
            titleList: [
                {
                    id: 1,
                    title: '项目',
                    key: 'xm'
                },
                {
                    id: 3,
                    title: '产业',
                    key: 'cy'
                },
                {
                    id: 4,
                    title: '产品',
                    key: 'cp'
                },
                {
                    id: 5,
                    title: '籍贯',
                    key: 'jg'
                },
                {
                    id: 6,
                    title: '区域',
                    key: 'dm'
                }
            ],
            pageType: '',
        }
    },
    components: {
        ProBlock,
        SearchHeader,
        CheckTitle,
        CompanyBlack,
        NoMessage
    },
    onLoad() {
    },
    onShow() {
        // 判断上一页面信息
        const pageInfo = getCurrentPages()
        pageInfo.findIndex(res => {
            console.log(res.route, 'route')
            if (res.route.indexOf('project') >= 0) {
                uni.setStorageSync('searchPage', 'project')
                this.pageType = 'project'
            } else if (res.route.indexOf('company') >= 0) {
                uni.setStorageSync('searchPage', 'company')
                this.pageType = 'company'
            } else {
                this.pageType = uni.getStorageSync('searchPage')
            }
        })
        console.log( this.pageType, ' this.pageType')
        if (this.pageType == 'project') {
            this.titleList[0] = {
                id: 1,
                title: '项目',
                key: 'xm'
            }
        } else {
            this.titleList[0] = {
                id: 1,
                title: '企业',
                key: 'firm'
            }
        }
    },
    methods: {
        navCompany(e) {
            uni.navigateTo({
                url: `../../../detail/pages/companyDetail/companyDetail?id=${e.id}`
            })
        },
        navProject(e) {
            uni.navigateTo({
                url: `../../../detail/pages/projectDetail/projectDetail?${e}`
            })
        },
        searchChange(e) {
            this.keyword = e
            this.resetting() // 清除分页状态
            this.getList()  // 获取数据
        },
        lower() {
            if (!this.pages.isOver) {
                this.pages.page++
                this.getList()
            }
        },
        // 获取企业列表
        getList() {
            const params = {
                uid: uni.getStorageSync('uid'),
                keyword: this.keyword,
                searchType: this.searchType,
                totalrevenue: 1,
                totalprofitrate: 1,
                registerdateorderby: 1,
                registeredcapitalorderby: 1,
                scaleorderby: 1,
                abroadorderby: 1,
                investmentorderby: 1,
                ratioorderby: 1,
                totalassets: 1
            }
            const pages = {
                p: this.pages.page,
                page: this.pages.size
            }
            if (this.pageType == 'project') {
                mobilesubscription(params, pages).then(res => {
                    this.dataList = this.dataList.concat(res.data.data)
                    this.pages.total = res.data.total
                    if (res.data.total < (res.data.page * res.data.p)) {
                        this.pages.isOver = true
                    }
                })
            } else if (this.pageType == 'company') {
                mobsearch(params, pages).then(res => {
                    this.dataList = this.dataList.concat(res.data.list)
                    this.pages.total = res.data.total
                    if (res.data.total < (res.data.page * res.data.p)) {
                        this.pages.isOver = true
                    }
                })                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  (params, pages)
            }
        },
        checkChange(e) {
            if (e == 999) return
            this.checkIndex = e
            this.searchType = this.titleList.find(res => res.id == e).key
        },

        // 重置分页信息
        resetting() {
            this.pages = {
                total: 0,
                size: 20,
                page: 1,
                isOver: false
            }
            this.dataList = []
        },
    }
})
</script>

<style scoped lang="scss">
.pro_main {
    width: 100%;
    padding: 12rpx 12rpx 0;
    box-sizing: border-box;
    background: #f8f8f8;
    .view_block {
        width: 100%;
        height: auto;
    }
}
</style>
